package com.p2p.front.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.p2p.base.model.Money;
import com.p2p.base.page.PageVo;
import com.p2p.front.dal.dao.TenderDao;
import com.p2p.front.dal.model.LoanInfo;
import com.p2p.front.dal.model.Tender;
import com.p2p.front.enums.TenderStatus;
import com.p2p.front.service.LoanInfoService;
import com.p2p.front.service.LoanVariationService;
import com.p2p.front.service.TenderService;
import com.p2p.front.tender.FlowLoanTender;
import com.p2p.front.tender.NormalLoanTender;
import com.p2p.front.util.RateUtil;
import com.p2p.user.facade.UserQueryFacade;

/**
 * @author ruiqi
 *
 */
@Service("tenderService")
public class TenderServiceImpl implements TenderService {

	private final Logger logger = LoggerFactory
			.getLogger(TenderServiceImpl.class);
	@Resource
	private TenderDao tenderDao;

	@Resource
	private FlowLoanTender flowLoanTender;

	@Resource
	private NormalLoanTender normalLoanTender;

	@Resource
	private UserQueryFacade userQueryFacade;

	@Resource
	private LoanVariationService loanVariationService;

	@Resource
	private LoanInfoService loanInfoService;

	@Override
	public void save(Tender tender) {
		tenderDao.create(tender);
	}

	@Override
	public PageVo myLoanList(PageVo form) {
		List<Map<String, Object>> list = tenderDao.userLoanList(form);
		if (list != null && list.size() > 0) {
			List<Map<String, Object>> loanList = loanInfoService
					.batchList(list);
			TenderReturnServiceImpl.merge(list, loanList, true);
			form.setList(list);
		}
		return form;
	}

	// loan_status,transfer_status 封装成 函数，更好~
	public List<Map<String, Object>> myWaitLoanList(String uid) {
		List<Map<String, Object>> waitLoanList = loanInfoService
				.batchWaitList();
		List<Map<String, Object>> list = null;
		if (waitLoanList != null && waitLoanList.size() > 0) {
			list = tenderDao.userWaitList(uid, waitLoanList);
			if (list != null && list.size() > 0) {
				TenderReturnServiceImpl.merge(list, waitLoanList, true);
			}
		}
		return list;
	}

	@Override
	public List<Tender> findByLoanId(Long lid) {
		List list = tenderDao.findByLoanId(lid);
		return list;
	}

	
	@Override
	public List<Tender> findSuccesedByLoanId(Long lid) {
		List list = tenderDao.findSuccesedByLoanId(lid);
		return list;
	}
	
	public List<Map<String, Object>> findAll(Long lid) {
		return tenderDao.findAll(lid);
	}

	@Override
	// 只是根据参数构造Tender对象，不保存到数据库中
	public Tender createTender(LoanInfo loanInfo, Long uid,
			String userAccountNo, String userName, Money amount, int auto,
			Integer autoRank) {
		Date currentTime = new Date();
		Money zeroMoney = new Money();
		Tender t = new Tender();
		t.setUid(uid);
		t.setUserName(userName);
		t.setUserAccount(userAccountNo);

		t.setAddtime(currentTime);
		t.setAmount(amount);
		t.setPayAmount(amount);
		t.setPaytime(currentTime);
		t.setAutoAmount(zeroMoney);

		t.setLid(loanInfo.getLid());
		t.setLoanTime(currentTime);
		t.setLoanRepayMethod(loanInfo.getRepayMethod());

		t.setLoanApr(loanInfo.getApr());

		Money allInterest = RateUtil.getAllInterest(loanInfo.getApr(),
				loanInfo.getDeadline(), amount, loanInfo.getRepayMethod());
		Money repayAmount = new Money(allInterest.getCent() + amount.getCent());
		t.setRepayAmount(repayAmount);
		t.setReturnRepayAmount(zeroMoney);

		t.setNum(loanInfo.getDeadline());
		t.setRepayNum(0);
		t.setStartNum(1);
		t.setReturnPrincipal(amount);
		t.setReturnRepayAmount(zeroMoney);

		if (loanInfo.getIsFlow() == 1) {
			t.setStatus(21);
		} else {
			t.setStatus(11);
		}

		t.setTstatus(0);
		if (auto == 1) {
			t.setAuto(1);
			t.setAutoRank(autoRank);
		} else {
			t.setAuto(0);
		}

		t.setBidtime(currentTime);
		t.setAddtime(currentTime);

		return t;
	}

	public List<Tender> findByUserId(String id) {
		return tenderDao.findByUserId(Long.valueOf(id));
	}

	@Override
	public Tender getByTid(Long tid) {
		return this.tenderDao.getByTid(tid);
	}

	@Override
	public Tender getByTidAndUid(Long tid, Long uid) {
		return this.tenderDao.getByTidAndUid(tid, uid);
	}

	@Override
	public boolean updateTenderStatus(Long tid, Integer status) {
		return 1 == this.tenderDao.updateTenderStatus(tid, status);
	}

	@Override
	public boolean updateTenderStatusForLoanFull(Long tid, Integer status,
			Date fullTime) {

		return 1 == this.tenderDao.updateTenderStatusForLoanFull(tid, status,
				fullTime);
	}

	@Override
	public boolean updateTenderTstatus(Long tid, Integer tstatus) {
		return 1 == this.tenderDao.updateTenderTstatus(tid, tstatus);
	}

	@Override
	public Map<String, Object> get(Long tid) {
		return tenderDao.get(tid);
	}

	@Override
	public List<Tender> findByLoanIdAndStatus(Long lid,
			TenderStatus tenderStatus) {
		return tenderDao.findByLoanIdAndStatus(lid, tenderStatus.getCode());
	}

	@Override
	public List<Tender> findByLoanIdAndStatusList(Long lid, List<Integer> statusList){
		return tenderDao.findByLoanIdAndStatusList(lid, statusList);
	}

	@Override
	public PageInfo queryPageByUserId(Long userId, int page, int rows) {
		PageHelper.startPage(page, rows);
		List<Tender> tenderList = tenderDao.findByUserId(userId);
		PageInfo pageInfo = new PageInfo(tenderList);
		return pageInfo;
	}

	@Override
	public Money sumTatalAmountByUid(String userId) {
		Long cent = this.tenderDao.sumTatalAmountByUid(userId);
		if (cent == null) {
			cent = 0L;
		}
		return new Money(cent);
	}

	@Override
	public int getTenderNumByLoanId(long lid) {
		return tenderDao.getCountByLid(lid);
	}

}
